home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / pbm / source / jpegv4.lha / jmemname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  8.0 KB  |  264 lines

  1. /*
  2.  * jmemname.c  (jmemsys.c)
  3.  *
  4.  * Copyright (C) 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file provides a generic implementation of the system-dependent
  9.  * portion of the JPEG memory manager.  This implementation assumes that
  10.  * you must explicitly construct a name for each temp file.
  11.  * Also, the problem of determining the amount of memory available
  12.  * is shoved onto the user.
  13.  */
  14.  
  15. #include "jinclude.h"
  16. #include "jmemsys.h"
  17.  
  18. #ifdef INCLUDES_ARE_ANSI
  19. #include <stdlib.h>             /* to declare malloc(), free() */
  20. #else
  21. extern void * malloc PP((size_t size));
  22. extern void free PP((void *ptr));
  23. #endif
  24.  
  25. #ifndef SEEK_SET                /* pre-ANSI systems may not define this; */
  26. #define SEEK_SET  0             /* if not, assume 0 is correct */
  27. #endif
  28.  
  29. #ifdef DONT_USE_B_MODE          /* define mode parameters for fopen() */
  30. #define READ_BINARY     "r"
  31. #define RW_BINARY       "w+"
  32. #else
  33. #define READ_BINARY     "rb"
  34. #define RW_BINARY       "w+b"
  35. #endif
  36.  
  37.  
  38. static external_methods_ptr methods; /* saved for access to error_exit */
  39.  
  40. static long total_used;         /* total memory requested so far */
  41.  
  42.  
  43. /*
  44.  * Selection of a file name for a temporary file.
  45.  * This is system-dependent!
  46.  *
  47.  * The code as given is suitable for most Unix systems, and it is easily
  48.  * modified for most non-Unix systems.  Some notes:
  49.  *  1.  The temp file is created in the directory named by TEMP_DIRECTORY.
  50.  *      The default value is /usr/tmp, which is the conventional place for
  51.  *      creating large temp files on Unix.  On other systems you'll probably
  52.  *      want to change the file location.  You can do this by editing the
  53.  *      #define, or by defining TEMP_DIRECTORY in CFLAGS in the Makefile.
  54.  *      For example, you might say
  55.  *          CFLAGS= ... '-DTEMP_DIRECTORY="/tmp/"'
  56.  *      Note that double quotes are needed in the text of the macro.
  57.  *      With most make systems you have to put single quotes around the
  58.  *      -D construct to preserve the double quotes.
  59.  *      (Amiga SAS C has trouble with ":" and such in command-line options,
  60.  *      so we've put in a special case for the preferred Amiga temp directory.)
  61.  *
  62.  *  2.  If you need to change the file name as well as its location,
  63.  *      you can override the TEMP_FILE_NAME macro.  (Note that this is
  64.  *      actually a printf format string; it must contain %s and %d.)
  65.  *      Few people should need to do this.
  66.  *
  67.  *  3.  mktemp() is used to ensure that multiple processes running
  68.  *      simultaneously won't select the same file names.  If your system
  69.  *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
  70.  *
  71.  *  4.  You probably want to define NEED_SIGNAL_CATCHER so that jcmain/jdmain
  72.  *      will cause the temp files to be removed if you stop the program early.
  73.  */
  74.  
  75. #ifndef TEMP_DIRECTORY          /* so can override from Makefile */
  76. #ifdef AMIGA
  77. #define TEMP_DIRECTORY  "JPEGTMP:"  /* recommended setting for Amiga */
  78. #else
  79. #define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
  80. #endif
  81. #endif
  82.  
  83. static int next_file_num;       /* to distinguish among several temp files */
  84.  
  85. #ifdef AMIGA
  86. LOCAL char *
  87. get_temp_dir (void)
  88. {
  89.     char *env;
  90.  
  91.     if( env = getenv("JPEGTMP") )
  92.         return env;
  93.     return TEMP_DIRECTORY;
  94. }
  95. #endif
  96.  
  97. #ifdef NO_MKTEMP
  98.  
  99. #ifndef TEMP_FILE_NAME          /* so can override from Makefile */
  100. #define TEMP_FILE_NAME  "%sJPG%03d.TMP"
  101. #endif
  102.  
  103. LOCAL void
  104. select_file_name (char * fname)
  105. {
  106.   FILE * tfile;
  107.  
  108.   /* Keep generating file names till we find one that's not in use */
  109.   for (;;) {
  110.     next_file_num++;            /* advance counter */
  111.     sprintf(fname, TEMP_FILE_NAME, get_temp_dir(), next_file_num);
  112.     if ((tfile = fopen(fname, READ_BINARY)) == NULL)
  113.       break;
  114.     fclose(tfile);              /* oops, it's there; close tfile & try again */
  115.   }
  116. }
  117.  
  118. #else /* ! NO_MKTEMP */
  119.  
  120. /* Note that mktemp() requires the initial filename to end in six X's */
  121. #ifndef TEMP_FILE_NAME          /* so can override from Makefile */
  122. #define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
  123. #endif
  124.  
  125. LOCAL void
  126. select_file_name (char * fname)
  127. {
  128.   next_file_num++;              /* advance counter */
  129.   sprintf(fname, TEMP_FILE_NAME, get_temp_dir(), next_file_num);
  130.   mktemp(fname);                /* make sure file name is unique */
  131.   /* mktemp replaces the trailing XXXXXX with a unique string of characters */
  132. }
  133.  
  134. #endif /* NO_MKTEMP */
  135.  
  136.  
  137. /*
  138.  * Memory allocation and freeing are controlled by the regular library
  139.  * routines malloc() and free().
  140.  */
  141.  
  142. GLOBAL void *
  143. jget_small (size_t sizeofobject)
  144. {
  145.   total_used += sizeofobject;
  146.   return (void *) malloc(sizeofobject);
  147. }
  148.  
  149. GLOBAL void
  150. jfree_small (void * object)
  151. {
  152.   free(object);
  153. }
  154.  
  155. /*
  156.  * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
  157.  * jget_large, jfree_large are not needed.
  158.  */
  159.  
  160.  
  161. /*
  162.  * This routine computes the total memory space available for allocation.
  163.  * It's impossible to do this in a portable way; our current solution is
  164.  * to make the user tell us (with a default value set at compile time).
  165.  * If you can actually get the available space, it's a good idea to subtract
  166.  * a slop factor of 5% or so.
  167.  */
  168.  
  169. #ifndef DEFAULT_MAX_MEM         /* so can override from makefile */
  170. #define DEFAULT_MAX_MEM         1000000L /* default: one megabyte */
  171. #endif
  172.  
  173. GLOBAL long
  174. jmem_available (long min_bytes_needed, long max_bytes_needed)
  175. {
  176.   return methods->max_memory_to_use - total_used;
  177. }
  178.  
  179.  
  180. /*
  181.  * Backing store (temporary file) management.
  182.  * Backing store objects are only used when the value returned by
  183.  * jmem_available is less than the total space needed.  You can dispense
  184.  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  185.  */
  186.  
  187.  
  188. METHODDEF void
  189. read_backing_store (backing_store_ptr info, void FAR * buffer_address,
  190.                     long file_offset, long byte_count)
  191. {
  192.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  193.     ERREXIT(methods, "fseek failed on temporary file");
  194.   if (JFREAD(info->temp_file, buffer_address, byte_count)
  195.       != (size_t) byte_count)
  196.     ERREXIT(methods, "fread failed on temporary file");
  197. }
  198.  
  199.  
  200. METHODDEF void
  201. write_backing_store (backing_store_ptr info, void FAR * buffer_address,
  202.                      long file_offset, long byte_count)
  203. {
  204.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  205.     ERREXIT(methods, "fseek failed on temporary file");
  206.   if (JFWRITE(info->temp_file, buffer_address, byte_count)
  207.       != (size_t) byte_count)
  208.     ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
  209. }
  210.  
  211.  
  212. METHODDEF void
  213. close_backing_store (backing_store_ptr info)
  214. {
  215.   fclose(info->temp_file);      /* close the file */
  216.   unlink(info->temp_name);      /* delete the file */
  217. /* If your system doesn't have unlink(), use remove() instead.
  218.  * remove() is the ANSI-standard name for this function, but if
  219.  * your system was ANSI you'd be using jmemansi.c, right?
  220.  */
  221. }
  222.  
  223.  
  224. GLOBAL void
  225. jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
  226. {
  227.   char tracemsg[TEMP_NAME_LENGTH+40];
  228.  
  229.   select_file_name(info->temp_name);
  230.   if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL) {
  231.     /* hack to get around ERREXIT's inability to handle string parameters */
  232.     sprintf(tracemsg, "Failed to create temporary file %s", info->temp_name);
  233.     ERREXIT(methods, tracemsg);
  234.   }
  235.   info->read_backing_store = read_backing_store;
  236.   info->write_backing_store = write_backing_store;
  237.   info->close_backing_store = close_backing_store;
  238.   /* hack to get around TRACEMS' inability to handle string parameters */
  239.   sprintf(tracemsg, "Using temp file %s", info->temp_name);
  240.   TRACEMS(methods, 1, tracemsg);
  241. }
  242.  
  243.  
  244. /*
  245.  * These routines take care of any system-dependent initialization and
  246.  * cleanup required.  Keep in mind that jmem_term may be called more than
  247.  * once.
  248.  */
  249.  
  250. GLOBAL void
  251. jmem_init (external_methods_ptr emethods)
  252. {
  253.   methods = emethods;           /* save struct addr for error exit access */
  254.   emethods->max_memory_to_use = DEFAULT_MAX_MEM;
  255.   total_used = 0;
  256.   next_file_num = 0;
  257. }
  258.  
  259. GLOBAL void
  260. jmem_term (void)
  261. {
  262.   /* no work */
  263. }
  264.